package com.dq.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dq.health.dao.notice.NoticeMapper;
import com.dq.health.dto.request.notice.QueryNoticeParam;
import com.dq.health.dto.request.notice.SaveNoticeRequest;
import com.dq.health.entity.notice.Notice;
import com.dq.health.enums.NoticeStatusEnum;
import com.dq.health.service.IAPINoticeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * <>
 *
 * @Author: chenxin
 * @Date: 2021/5/17
 */
@Service
public class NoticeServiceImpl implements IAPINoticeService {

    @Resource
    NoticeMapper noticeMapper;

    @Override
    public Long addNotice(SaveNoticeRequest request) {
        Assert.notNull(request.getTitle(), "title is cant be null");
        Assert.notNull(request.getContent(), "content is cant be null");
        Assert.notNull(request.getNoticeType(), "noticeType is cant be null");
        Assert.notNull(request.getOperationUserId(), "operationUserId is cant be null");

        Notice addNotice = new Notice();
        addNotice.setTitle(request.getTitle());
        addNotice.setContent(request.getContent());
        addNotice.setNoticeType(request.getNoticeType());
        addNotice.setNoticeStatus(NoticeStatusEnum.WAIT.getValue());
        addNotice.setCreateUserId(request.getOperationUserId());
        noticeMapper.insert(addNotice);

        return addNotice.getNoticeId();
    }

    @Override
    public void updateNotice(SaveNoticeRequest request) {
        Assert.notNull(request.getNoticeId(), "noticeId is cant be null");
        Assert.notNull(request.getOperationUserId(), "operationUserId is cant be null");

        Notice updateNotice = new Notice();
        BeanUtils.copyProperties(request, updateNotice);
        updateNotice.setUpdateUserId(request.getOperationUserId());
        noticeMapper.updateById(updateNotice);
    }

    @Override
    public Notice getNoticeById(Long id) {
        return noticeMapper.selectById(id);
    }

    @Override
    public IPage<Notice> queryPageList(QueryNoticeParam param) {
        IPage<Notice> page = new Page<>(param.getPage(), param.getPageSize());

        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        if (param.getNoticeStatus() != null) {
            queryWrapper.eq(Notice::getNoticeStatus, param.getNoticeStatus());
        } else {
            queryWrapper.ne(Notice::getNoticeStatus, 2);
        }
        if (param.getNoticeType() != null) {
            queryWrapper.eq(Notice::getNoticeType, param.getNoticeType());
        }
        if (StringUtils.isBlank(param.getKeyword())) {
            queryWrapper.like(Notice::getTitle, "%" + param.getKeyword() + "%");
        }

        return noticeMapper.selectPage(page, queryWrapper);
    }

}
